﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace ByrForum
{
    public class HttpUtil
    {
        public static string ReadToEnd(IAsyncResult asynchronousResult)
        {
            string result = "", rr = "";
            HttpWebRequest hwrq = (HttpWebRequest)asynchronousResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)hwrq.EndGetResponse(asynchronousResult);
            Debug.WriteLine("get callback");
            Stream responseStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(responseStream);
            rr = sr.ReadToEnd();
            DebugInfo.writeXml(result);
            result = SanitizeXmlString(rr);
            sr.Close();
            responseStream.Close();
            response.Close();
            return result;
        }
        public static string ReadToEndWithParam(IAsyncResult asynchronousResult)
        {
            string result = "", rr = "";
            RequestState state = (RequestState)asynchronousResult.AsyncState;
            HttpWebRequest hwrq = state.request;
            HttpWebResponse response = (HttpWebResponse)hwrq.EndGetResponse(asynchronousResult);
            Debug.WriteLine("get callback");
            Stream responseStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(responseStream);
            rr = sr.ReadToEnd();
            DebugInfo.writeXml(result);
            result = SanitizeXmlString(rr);
            sr.Close();
            responseStream.Close();
            response.Close();
            return result;
        }
        public static void HttpGet(string url, AsyncCallback callback)
        {
            try
            {
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
                req.Credentials = new NetworkCredential(App.MyInfo.id, App.MyInfo.password);
                req.BeginGetResponse(callback, req);

            }
            catch
            {
                Debug.WriteLine("network failed");
            }
        }
        public static void HttpGetWithParam(RequestState state,string url, AsyncCallback callback)
        {
            try
            {
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
                
                req.Credentials = new NetworkCredential(App.MyInfo.id, App.MyInfo.password);
                state.request = req;
                req.BeginGetResponse(callback, state);

            }
            catch
            {
                Debug.WriteLine("network failed");
            }
        }

        public static void HttpPost(string url, AsyncCallback callback)
        {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.Credentials = new NetworkCredential(App.MyInfo.id, App.MyInfo.password);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.BeginGetRequestStream(callback, req);
        }

        public static string SanitizeXmlString(string xml)
        {
            
            if (xml == null)
            {
                throw new ArgumentNullException("xml");
            }

            StringBuilder buffer = new StringBuilder(xml.Length);

            foreach (char c in xml)
            {
                if (c != 0x1B && c!=0x10)
                {
                    buffer.Append(c);
                }
            }
            return buffer.ToString();

        }
    }
}
